#include "Common.h"

Item::Item(ui32 highguid, ui32 lowguid)
{
	m_ui32values = _fields;
	m_fieldcount = ITEM_END;
	m_updatemask = UpdateMask();
	m_updatemask.SetCount(ITEM_END);
	memset(m_ui32values, 0, ITEM_END * 4);
	SetUI32Value(OBJECT_FIELD_GUID, lowguid);
	SetUI32Value(OBJECT_FIELD_GUID + 1, highguid);
	m_typeid = TYPEID_ITEM;
	m_guid = WoWGuid(GetUI64Value(OBJECT_FIELD_GUID));
	SetUI32Value(OBJECT_FIELD_TYPE, 2 | 1);
	SetFloatValue(OBJECT_FIELD_SCALE_X, 1);
}

Item::Item(ui32 guid)
{
	m_ui32values = _fields;
	m_fieldcount = ITEM_END;
	m_updatemask = UpdateMask();
	m_updatemask.SetCount(ITEM_END);
	memset(m_ui32values, 0, ITEM_END * 4);
	SetUI32Value(OBJECT_FIELD_GUID, guid);
	SetUI32Value(OBJECT_FIELD_GUID + 1, HIGHGUID_TYPE_ITEM);
	m_typeid = TYPEID_ITEM;
	m_guid = WoWGuid(GetUI64Value(OBJECT_FIELD_GUID));
	SetUI32Value(OBJECT_FIELD_TYPE, 2 | 1);
	SetFloatValue(OBJECT_FIELD_SCALE_X, 1);
}

Item::~Item()
{
	miscmgr->RemoveItem(GetGUID());
}

bool Item::LoadFromDb()
{
	ui64 guid = GetGUID();
	QResult* res = CharacterDatabase.SafeQuery("Select * from playeritems where itemguid = %u", (ui32)guid);
	if(!res || !res->NumRows())
		return false;
	QField* rw = res->Fetch();
	ui64 playerguid = rw[0];
	Player* tmp = miscmgr->GetPlayer(playerguid);
	if(!tmp)
	{
		delete res;
		return false;
	}
	m_owner = tmp;
	ui32 itemid = rw[2].GetUI32();
	ItemInfo* inf = miscmgr->GetItemInfo(itemid);
	if(!inf)
	{
		delete res;
		return false;
	}
	SetInfo(inf);
	delete res;
	return true;
}


void Item::SetOwner(Player* pPlayer)
{
	SetUI64Value(ITEM_FIELD_OWNER, pPlayer->GetGUID());
	m_owner = pPlayer;
}

void Item::SaveToDb()
{
	stringstream strm;
	strm << "Replace into playeritems VALUES ("
		<< GetUI32Value(ITEM_FIELD_OWNER) << " , " << (ui32)GetGUID() << " , " << GetUI32Value(OBJECT_FIELD_ENTRY) << " , NULL)";
	CharacterDatabase.Query(strm.str().c_str());
}

void Item::UpdateFields()
{
	if(!m_info)
		return;
	SetUI32Value(OBJECT_FIELD_ENTRY, m_info->ItemId);
	if(m_owner)
	{
		SetUI64Value(ITEM_FIELD_OWNER, m_owner->GetGUID());
		SetUI64Value(ITEM_FIELD_CONTAINED, m_owner->GetGUID());
	}
	SetUI32Value(ITEM_FIELD_STACK_COUNT, 1);
	SetUI32Value(ITEM_FIELD_SPELL_CHARGES, m_info->Spells[0].Charges);
	SetUI32Value(ITEM_FIELD_SPELL_CHARGES_1, m_info->Spells[1].Charges);
	SetUI32Value(ITEM_FIELD_SPELL_CHARGES_2, m_info->Spells[2].Charges);
	SetUI32Value(ITEM_FIELD_SPELL_CHARGES_3, m_info->Spells[3].Charges);
	SetUI32Value(ITEM_FIELD_SPELL_CHARGES_4, m_info->Spells[4].Charges);
	SetUI32Value(ITEM_FIELD_MAXDURABILITY, m_info->MaxDurability);
	SetUI32Value(ITEM_FIELD_DURABILITY, m_info->MaxDurability);
}

const ItemProf* GetProficiencyBySkill(ui32 skill)
{
	switch(skill)
	{
		case SKILL_CLOTH:
			return &prof[0];
		case SKILL_LEATHER:
			return &prof[1];
		case SKILL_MAIL:
			return &prof[2];
		case SKILL_PLATE_MAIL:
			return &prof[3];
		case SKILL_SHIELD:
			return &prof[4];
		case SKILL_AXES:
			return &prof[5];
		case SKILL_2H_AXES:
			return &prof[6];
		case SKILL_BOWS:
			return &prof[7];
		case SKILL_GUNS:
			return &prof[8];
		case SKILL_MACES:
			return &prof[9];
		case SKILL_2H_MACES:
			return &prof[10];
		case SKILL_POLEARMS:
			return &prof[11];
		case SKILL_SWORDS:
			return &prof[12];
		case SKILL_2H_SWORDS:
			return &prof[13];
		case SKILL_STAVES:
			return &prof[14];
		case SKILL_FIST_WEAPONS:
			return &prof[15];
		case SKILL_DAGGERS:
			return &prof[16];
		case SKILL_THROWN:
			return &prof[17];
		case SKILL_SPEARS:
			return &prof[18];
		case SKILL_CROSSBOWS:
			return &prof[19];
		case SKILL_WANDS:
			return &prof[20];
		case SKILL_FISHING:
			return &prof[21];
		default:
			return NULL;
	}
}